def capitalize_words(input_file, output_file):
    try:
        # 读取源文件内容
        with open(input_file, 'r', encoding='utf-8') as file:
            content = file.read()

        # 处理内容，将每个英文单词的首字母大写
        processed_content = []
        current_word = []
        in_word = False

        for char in content:
            if char.isalpha():
                if not in_word:
                    in_word = True
                    # 将当前字符转换为大写
                    current_word.append(char.upper())
                else:
                    current_word.append(char)
            else:
                # 非字母字符，结束当前单词
                if in_word:
                    processed_content.append(''.join(current_word))
                    current_word = []
                    in_word = False
                processed_content.append(char)

        # 处理最后一个单词（如果文件以字母结尾）
        if in_word:
            processed_content.append(''.join(current_word))

        processed_text = ''.join(processed_content)

        # 将处理后的内容写入目标文件
        with open(output_file, 'w', encoding='utf-8') as file:
            file.write(processed_text)

        print(f"成功处理文件，结果已保存到 {output_file}")

    except FileNotFoundError:
        print(f"错误：找不到文件 {input_file}")
    except Exception as e:
        print(f"发生错误：{e}")


if __name__ == "__main__":
    # 输入和输出文件路径
    source_file = "datas/source.txt"
    target_file = "datas/target.txt"

    # 执行文本处理
    capitalize_words(source_file, target_file)
